// src/services/api.ts
import axios from 'axios';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080';

const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
});
apiClient.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers['Authorization'] = `Bearer ${token}`;
  }
  return config;
}, error => {
  return Promise.reject(error);
});
//登录
export const login = (credentials: { username: string; password: string }) => {
  return apiClient.post('/api/auth/login', credentials);
};
//注册
export const register = (userData: { username: string; password: string}) => {
  return apiClient.post('/api/user', userData);
};
//加载房间
export const loadRooms = async () => {
  return apiClient.get('/api/room/getRoom');
};

//加入房间
export const joinRoom = async (credentials: { roomId: string; password: string }) => {
  const { roomId, password } = credentials; // 解构 credentials 对象
  return apiClient.post(`/api/room/join`, { roomId, password }); // 指定正确的请求路径和请求体
};

// 创建房间
export const createRoom = async (roomData: {
  password: string;
  maxPlayers: number; // 确保传入的 maxPlayers 是数字类型
}) => {
  return apiClient.post('/api/room/create', roomData);
};

// 快速加入：发送加入匹配队列请求
export const quickJoinMatchmaking = async () => {
  return apiClient.post('/api/matchmaking/join');
};

// 获取匹配状态
export const getMatchmakingStatus = async () => {
  return apiClient.get('/api/matchmaking/status');
};

// 获取排行榜
export const getLeaderboard = async (page: number, size: number) => {
  return apiClient.get(`/api/leaderboard?page=${page}&size=${size}`);
};

// 获取个人排名
export const getMyRanking = async () => {
  return apiClient.get('/api/leaderboard/my-ranking');
};

// 其他接口...